﻿using DanTriNews.Src.Helper;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace DanTriNews.ViewModels
{
    public class CategoryViewModel : INotifyPropertyChanged
    {
        public CategoryViewModel(int id,string categoryName,string url)
        {
            Page = 1;
            Id = id;
            CategoryName = categoryName;
            Url = url;
        }
        private int _id;
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                if (value != _id)
                {
                    _id = value;
                    NotifyPropertyChanged("Id");
                }
            }
        }
        private string _categoryName;
        public string CategoryName
        {
            get
            {
                return _categoryName;
            }
            set
            {
                if (value != _categoryName)
                {
                    _categoryName = value;
                    NotifyPropertyChanged("CategoryName");
                }
            }
        }
        private string _url;
        public string Url
        {
            get
            {
                return _url;
            }
            set
            {
                if (value != _url)
                {
                    _url = value;
                    NotifyPropertyChanged("Url");
                }
            }
        }
        private int _page;
        public int Page
        {
            get
            {
                return _page;
            }
            set
            {
                if (value != _page)
                {
                    _page = value;
                    NotifyPropertyChanged("Page");
                }
            }
        }
        public ObservableCollection<NewsViewModel> Newses { get; private set; }

        public bool IsDataLoaded = false;
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public async void Loaddata()
        {
            if(!IsDataLoaded)
            {
                MainPage.instance.prbLoad.Visibility = Visibility.Visible;
                Newses =await NewsHelper.GetNews(Id, Page);
                NotifyPropertyChanged("Newses");
                IsDataLoaded = true;
                MainPage.instance.prbLoad.Visibility = Visibility.Collapsed;
            }
        }
        public void Next()
        {
            Page++;
            IsDataLoaded = false;
            Loaddata();
        }
        public void ReLoadData()
        {
            IsDataLoaded = false;
            Loaddata();
        }

        internal void Prev()
        {
            if(Page>1)
            {
                Page--;
                IsDataLoaded = false;
                Loaddata();
            }
        }
        public NewsViewModel FindNews(int Id)
        {
            foreach(NewsViewModel n in Newses)
            {
                if (n.Id == Id)
                    return n;
            }
            return null;
        }

        internal NewsViewModel FindNextNews(int newsId)
        {
            try
            {
                NewsViewModel nv = FindNews(newsId);
                int k = Newses.IndexOf(nv);
                if (k > -1)
                    return Newses[k + 1];
                else
                    return null;
            }
            catch
            {
                return null;
            }
        }

        internal NewsViewModel FindPrevNews(int newsId)
        {
            try
            {
                NewsViewModel nv = FindNews(newsId);
                int k = Newses.IndexOf(nv);
                if (k > 0)
                    return Newses[k - 1];
                else
                    return null;
            }
            catch
            {
                return null;
            }
        }
    }
}